/*
 * $Id$
 */
package corejs.expr;

import corejs.rt.ActivationRecord;
import corejs.rt.Value;
import corejs.util.XmlPrinter;
import java.util.ArrayList;
import java.util.List;

/**
 *
 * @author myang
 */
public class InvocationExpr implements Expression {

   private Expression _expr;
   private List<Expression> _args;

   public InvocationExpr(final Expression fn, final Expression arg) {
      _expr = fn;
      _args = ExpressionUtil.toList(new ArrayList<Expression>(), arg);
   }

   public Expression expr() {
      return _expr;
   }

   public Expression arg(final int index) {
      return _args != null && (0 <= index && index < _args.size())
              ? _args.get(index) : null;
   }

   @Override
   public void print(final XmlPrinter printer) {
      printer.open("calling").print(_expr);
      if (0 == _args.size()) {
         printer.print("args");
      } 
      else {
         printer.open("args");
         for (final Expression arg : _args) {
            printer.print(arg);
         }
         printer.close();
      }
      printer.close();
   }

   @Override
   public Object eval(ActivationRecord scope) {
      return null;
   }

   @Override
   public Value accept(final ExpressionVisitor visitor) {
      return visitor.visitInvocation(_expr, _args);
   }
}
